package com.sun.es.controller;

import com.sun.es.entity.Book;
import com.sun.es.service.BookService;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BookController {
    private final BookService bookService;
    
    public BookController(BookService bookService) {
        this.bookService = bookService;
    }

    @GetMapping("/book/{num}")
    public Map<String, String> addBook(@PathVariable Integer num) {
        for(int i = 0; i < num; i++) {
            int count = i + 1;
            bookService.addBook(Book.builder()
                            .id(String.valueOf(count))
                            .title("Book Title" + count)
                            .author("Book Author" + count)
                            .price(1.23 + count)
                            .createTime(new Date())
                            .build());
        }
        Map<String,String> map = new HashMap<>();
        map.put("msg", "ok");
        return map;
    }

    @GetMapping("/book/findAll")
    public List<Book> findAll() {
        return bookService.findAll();
    }

    @GetMapping("/book/deleteAll")
    public Map<String, String> deleteAll() {
        bookService.deleteAll();
        Map<String,String> map = new HashMap<>();
        map.put("msg", "delete success");
        return map;
    }

}
